package com.erp.erp_ui.WjCatalog;

import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.WjCatalog.WjCatalogEntity;
import com.erp.erp_entitys.WjCatalog.WjCatalogExample;
import com.erp.erp_entitys.WjCatalog.WjCatalogVoEntity;
import com.erp.erp_entitys.WjInfo.WjInfoEntity;
import com.erp.erp_servers.WjCatalog.IWjCatalogService;
import com.erp.erp_servers.WjInfo.IWjInfoService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName WjCatalogControl
 * @Description 文件目录
 * @Author songjizhen
 * @Date 2020-06-30 21:41
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "wjcatalog")
public class WjCatalogControl extends Base {
    @Autowired
    IWjCatalogService wjCatalogService;
    @Autowired
    IWjInfoService wjInfoService;
    /**
     * 文件
     * @param
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody
    public R<List<WjCatalogEntity>> selectByPage(){
        try{
            return R.build(null);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 文件树
     * @return
     */
    @RequestMapping(value = "select")
    @ResponseBody
    public R<WjCatalogVoEntity> select(){
        try{
            WjCatalogExample example=new WjCatalogExample();
            WjCatalogExample.Criteria criteria = example.createCriteria();
            List<WjCatalogEntity> wjCatalogEntities = wjCatalogService.selectByExample(example);
            WjCatalogVoEntity vo=new WjCatalogVoEntity();
            vo.setCode("0");
            vo.setId(0);
            vo.setName("集团公司");
            vo.setPath("/");
            vo.setChildren(this.converTree(0,wjCatalogEntities));
            return R.build(vo);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    private List<WjCatalogVoEntity> converTree(Integer pid, List<WjCatalogEntity> pce){
        List<WjCatalogVoEntity>  list=new ArrayList<WjCatalogVoEntity>();
        for(int i=0;i<pce.size();i++){
            WjCatalogEntity pc = pce.get(i);
            if(pc.getPid()==pid){
                WjCatalogVoEntity vo=new WjCatalogVoEntity();
                vo.setId(pc.getId());
                vo.setCode(pc.getCode());
                vo.setName(pc.getName());
                vo.setPath(pc.getPath());
                vo.setPid(pc.getPid());
                vo.setRemark(pc.getRemark());
                vo.setChildren(this.converTree(vo.getId(),pce));
                list.add(vo);
            }
        }
        return list;
    }

    /**
     * 树删除节点
     * @param ids
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> delete(String[] ids){
        try{
            List<Integer> list=new ArrayList<>();
            for(int i=0;i<ids.length;i++){
                list.add( Integer.parseInt(ids[i].replace("[","").replace("]","")));
            }
            WjCatalogExample example =new WjCatalogExample();
            WjCatalogExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(list);
            int num = wjCatalogService.deleteByExample(example);
            return  R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 删除以path开头的
     * @param path
     * @return
     */
    @RequestMapping(value = "deleteByPath",method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> deleteByPath(String path){
        try {
            WjCatalogExample example=new WjCatalogExample();
            WjCatalogExample.Criteria criteria = example.createCriteria();
            criteria.andPathLike("%"+path+"%");
            int num = wjCatalogService.deleteByExample(example);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 获取父节点信息
     * @param
     * @return
     */
    @RequestMapping(value = "queryPid",method = RequestMethod.GET)
    @ResponseBody
    public R<List<WjCatalogEntity> > queryPid(){
        try{
            WjCatalogExample example=new WjCatalogExample();
            WjCatalogExample.Criteria criteria = example.createCriteria();
            // example.setOrderByClause("status");
            List<String> list=new ArrayList<>();
            list.add("/0/4");
            list.add("/0/5");
            list.add("/");
            criteria.andPathNotIn(list);
            List<WjCatalogEntity> wjCatalogEntities = wjCatalogService.selectByExample(example);

            return  R.build(wjCatalogEntities);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 新增文件目录
     * @param pce
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
    public  R<Integer> insert(@RequestBody WjCatalogEntity pce){
        try{
            if(pce.getId() !=null){
                return  R.build(this.update(pce));
            }
            WjCatalogEntity p = wjCatalogService.selectByPrimaryKey(pce.getPid());
            pce.setPath(p.getPath()+"/"+pce.getCode());
            int num = wjCatalogService.insertSelective(pce);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 新增文件目录,用于在客户、供应商模块中的文件
     * @param pce
     * @return
     */
    @RequestMapping(value = "saveOrUpdate",method = RequestMethod.POST)
    @ResponseBody
    public  R<Integer> saveOrUpdate(@RequestBody WjCatalogEntity pce){
        try{
            String filepath=pce.getPath();
            //查询文件目录是否存在
            WjCatalogExample example=new WjCatalogExample();
            WjCatalogExample.Criteria criteria = example.createCriteria();
            criteria.andCodeEqualTo(pce.getCode());
            List<WjCatalogEntity> selectByExample=wjCatalogService.selectByExample( example);
            //如果存在，则插入文件，否则先创建目录再插入文件
            if(selectByExample.size()>0){

               int num= this.saveFile(filepath,selectByExample.get(0).getCode());
                return R.build(num);
            }else{
                pce.setPath("/0/3/"+pce.getCode());
                int num = wjCatalogService.insertSelective(pce);
                num=this.saveFile(filepath,pce.getCode());
                return R.build(num);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }
    private int  saveFile(String filePath,String cataLogCode){
        if(filePath.equals("")){
            return 1;
        }
        filePath=filePath.replace("[","").replace("]","").replace("\"","");
        UserEntity user=(UserEntity)this.getUser();
        int num=0;
        WjInfoEntity record=new WjInfoEntity();
        String[] split = filePath.split(",");
        for(int i=0;i<split.length;i++){
            String[] objs=split[i].split("###") ;
            String path=objs[1].split("pathname=")[1].replace("&filename=","/");
            record.setPath(path);
            record.setWjCatalogCode(cataLogCode);
            record.setName(objs[0]);
            record.setUptime(new Date());
            //record.setUpuser(user.getId().);
            num=wjInfoService.insertSelective(record);
        }
        return num;
    }
    private Integer update(WjCatalogEntity pce){
        int num = wjCatalogService.updateByPrimaryKeySelective(pce);
        return num;
    }
}
